const express = require('express')
const router = express.Router()
const pool = require('../pool')

module.exports = router


	//-----------------将数据插入数据库------------------	
router.get('/show',(req,res)=>{
	res.send('test')
	console.log('test')
})	
	
//		http://localhost:3000/v10/user/add   地址
	// --------------------------将用户数据插入到数据库中	
router.post('/add',(req,res,next)=>{
	let user = req.body.user
	let utime = req.body.utime
	let correct = req.body.correct
	let speed = req.body.speed
			
	let sql = 'insert into js_user set user=?,utime=?,correct=?,speed=?'
		
	pool.query(sql,[user,utime,correct,speed],(err,result)=>{
		if(err){
			return next(err)
		}

		if(result.affectedRows===0){
			res.send({code:501,msg:'注册失败'})
		}else{
			res.send({code:200,msg:'注册成功'})
		}	
		
	})
})

//http://localhost:3000/v10/user/list     地址
// --------------------根据查询条件-----------将从数据库中查询的数据显示到页面-------------
router.post('/list',(req,res,next)=>{
	let user = req.body.user
	let sql = 'select * from js_user where user=?'
	pool.query(sql,[user],(err,result)=>{
		if(err){
			return next(err)
		}
		
		if(result.length>0){
			res.send({code:200,msg:'select success 成功',data:result})
			
		}else{
			res.send('select failed 失败')
		}
		
		
	})

})





